<template>
  <div id="ele-app">
    <div id="ele-container">
      <div class="header">
        <img
          src="./assets/appicon.png"
          alt=""
          style="width: 20px; height: 20px; margin: 5px"
        />
        <span class="maintitle">Electron-Vue-Ant v{{ version }}</span>
        <ul class="controlbtn">
          <li @click="minwin">
            <a-icon type="line" />
          </li>
          <li @click="maxwin">
            <a-icon type="border" />
          </li>
          <li @click="closewin">
            <!-- <i class="iconfont icon-closebtn" id="closebtn"></i> -->
            <a-icon type="close" />
          </li>
        </ul>
      </div>
      <Ohyeah>
        <div class="contenter" style="margin-top: 30px">
          <a-config-provider
            :locale="locale"
            :get-popup-container="popContainer"
          >
            <router-view />
          </a-config-provider>
        </div>
         </Ohyeah>
      <div class="electron-footer">
        <div style="display: flex">
          <a-progress
            :percent="percentage"
            size="small"
            style="width: 100px; margin-top: 4px; margin-right: 10px"
            status="active"
            v-if="downloadinfo"
          /><span class="info">{{ updateTips }}</span>
        </div>
        <div class="copy">&copy; Yourweb.com</div>
      </div>
    </div>
  </div>
</template>

<script>
//const { ipcRenderer } = require("electron");
import { enquireScreen } from './utils/util';
import { mapState, mapMutations } from 'vuex';
import themeUtil from '@/utils/themeUtil';
import { getI18nKey } from '@/utils/routerUtil';
const { ipcRenderer } = require('electron');
// import Mainpage from "./page/mainpage.vue";
import config from '../package.json';

export default {
  name: 'App',
  data() {
    return {
      themeMode: 'normal',
      updateTips: '',
      downloadinfo: false,
      percentage: 0,
      version: '',
      locale: {},
    };
  },
  created() {
    this.setHtmlTitle();
    this.setLanguage(this.lang);
    enquireScreen((isMobile) => this.setDevice(isMobile));
  },
  watch: {
    weekMode(val) {
      this.setWeekModeTheme(val);
    },
    lang(val) {
      this.setLanguage(val);
      this.setHtmlTitle();
    },
    $route() {
      this.setHtmlTitle();
    },
    'theme.mode': function (val) {
      let closeMessage = this.$message.loading(
        `您选择了主题模式 ${val}, 正在切换...`
      );
      themeUtil.changeThemeColor(this.theme.color, val).then(closeMessage);
    },
    'theme.color': function (val) {
      let closeMessage = this.$message.loading(
        `您选择了主题色 ${val}, 正在切换...`
      );
      themeUtil.changeThemeColor(val, this.theme.mode).then((closeMessage));
    },
    layout: function () {
      window.dispatchEvent(new Event('resize'));
    },
  },
  computed: {
    ...mapState('setting', ['layout', 'theme', 'weekMode', 'lang']),
  },
  mounted() {
    this.setWeekModeTheme(this.weekMode);
    let _this = this;
    _this.version = config.version;
    //接收主进程版本更新消息
    ipcRenderer.on('message', (event, arg) => {
      // for (var i = 0; i < arg.length; i++) {
      console.log(arg);
      if ('update-available' == arg.cmd) {
        //显示升级对话框
        _this.$message.info('发现新的版本');
        _this.updateTips = '发现新版本';
        _this.dialogVisible = true;
      } else if ('download-progress' == arg.cmd) {
        _this.updateTips = '下载中...';
        _this.downloadinfo = true;
        //更新升级进度
        /*
	      message{bytesPerSecond: 47673
	      delta: 48960
	      percent: 0.11438799862426002
	      total: 42801693
	      transferred: 48960
	      }
	     */
        console.log(arg.message.percent);
        let percent = Math.round(parseFloat(arg.message.percent));
        _this.percentage = percent;
      } else if ('error' == arg.cmd) {
        _this.dialogVisible = false;
        _this.$message.error('更新失败');
      } else if ('update-downloaded' == arg.cmd) {
        //_this.$message.info("下载完成");
        _this.downloadinfo = false;
        _this.updateTips = '';
        _this.$confirm({
          content: <div>下载完成，是否立即重启？</div>,
          onOk() {
            console.log('OK');
            ipcRenderer.send('resetApp');
          },
          onCancel() {
            console.log('Cancel');
          },
        });
      }
      // }
    });
    ipcRenderer.send('checkForUpdate');
  },
  components: {
    //Mainpage,
  },
  methods: {
    ...mapMutations('setting', ['setDevice']),
    minwin() {
      console.log('minwin');
      ipcRenderer.send('minMainWin');
    },
    maxwin() {
      console.log('maxwin');
      ipcRenderer.send('maxMainWin');
    },
    closewin() {
      console.log('closewin');
      ipcRenderer.send('closeMainWin');
    },
    setWeekModeTheme(weekMode) {
      if (weekMode) {
        document.body.classList.add('week-mode');
      } else {
        document.body.classList.remove('week-mode');
      }
    },
    setLanguage(lang) {
      this.$i18n.locale = lang;
      switch (lang) {
        case 'CN':
          this.locale =
            require('ant-design-vue/es/locale-provider/zh_CN').default;
          break;
        case 'HK':
          this.locale =
            require('ant-design-vue/es/locale-provider/zh_TW').default;
          break;
        case 'US':
        default:
          this.locale =
            require('ant-design-vue/es/locale-provider/en_US').default;
          break;
      }
    },
    setHtmlTitle() {
      const route = this.$route;
      const key =
        route.path === '/'
          ? 'home.name'
          : getI18nKey(route.matched[route.matched.length - 1].path);
      document.title = process.env.VUE_APP_NAME + ' | ' + this.$t(key);
    },
    popContainer() {
      return document.getElementById('popContainer');
    },
  },
};
</script>

<style lang="less">
body {
  overflow: hidden;
}
ul,
li {
  margin: 0;
  padding: 0;
}
ul {
  list-style: none;
}
#ele-container img {
  width: 600px;
}
.logo {
  text-align: center;
}
#ele-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  -webkit-app-region: drag;
  background-color: #3a3a3a;
  height: 30px;
  display: flex;
  justify-content: space-between;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 9999999;
}
.header span {
  color: #ccc;
  line-height: 30px;
  padding: 0 10px;
}
.contenter {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
}
.electron-footer {
  line-height: 30px;
  display: flex;
  padding: 0 13px;
  justify-content: flex-end;
  font-size: 12px;
}
.controlbtn {
  color: #fff;
  float: right;
  line-height: 30px;
}
.controlbtn li {
  cursor: pointer;
  float: left;
  height: 30px;
  padding: 0px 14px;
  -webkit-app-region: no-drag;
}
.controlbtn li:hover {
  background: #444;
}
.controlbtn li:nth-child(3):hover {
  background: #c70404;
}

.mycard .ant-card-body {
  padding: 10px;
  padding-top: 0 !important;
}
.controlbtn .iconfont {
  font-size: 14px;
}

* {
  -webkit-touch-callout: none; /*系统默认菜单被禁用*/
  -webkit-user-select: none; /*webkit浏览器*/
  -khtml-user-select: none; /*早期浏览器*/
  -moz-user-select: none; /*火狐*/
  -ms-user-select: none; /*IE10*/
  user-select: none;
}
input {
  -webkit-user-select: auto; /*webkit浏览器*/
}
textarea {
  -webkit-user-select: auto; /*webkit浏览器*/
}
</style>
